//===- SVTypes.cpp - Implement the SV types -------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implement the SV dialect type system.
//
//===----------------------------------------------------------------------===//

#include "circt/Dialect/SV/SVTypes.h"
#include "circt/Dialect/HW/HWTypes.h"
#include "circt/Dialect/SV/SVDialect.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/DialectImplementation.h"
#include "llvm/ADT/TypeSwitch.h"

using namespace circt;
using namespace circt::sv;

/// Return the element type of an ArrayType or UnpackedArrayType, or null if the
/// operand isn't an array.
Type circt::sv::getAnyHWArrayElementType(Type type) {
  if (!type)
    return {};
  if (auto array = hw::type_dyn_cast<hw::ArrayType>(type))
    return array.getElementType();
  if (auto array = hw::type_dyn_cast<hw::UnpackedArrayType>(type))
    return array.getElementType();

  return {};
}

//===----------------------------------------------------------------------===//
// InOut type logic.
//===----------------------------------------------------------------------===//

/// Return the element type of an InOutType or null if the operand isn't an
/// InOut type.
mlir::Type circt::sv::getInOutElementType(mlir::Type type) {
  if (auto inout = dyn_cast_or_null<InOutType>(type))
    return inout.getElementType();
  return {};
}

//===----------------------------------------------------------------------===//
// TableGen generated logic.
//===----------------------------------------------------------------------===//

// Provide the autogenerated implementation guts for the Op classes.
#define GET_TYPEDEF_CLASSES
#include "circt/Dialect/SV/SVTypes.cpp.inc"

void SVDialect::registerTypes() {
  addTypes<
#define GET_TYPEDEF_LIST
#include "circt/Dialect/SV/SVTypes.cpp.inc"
      >();
}
